package lesson6.socket.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.time.LocalDateTime;

/**
 * 接收服务器内容<br/>
 * Created on : 2018-01-28 18:40
 * @author lizebin
 */
public class ClientHandler extends SimpleChannelInboundHandler<String> {

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
		Thread.sleep(1000);
		System.out.println("client output remote address:" + ctx.channel().remoteAddress());
		System.out.println("client output : " + msg);
		ctx.writeAndFlush("from client: " + LocalDateTime.now());
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.channel().close();
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("activite!");
		//客户端发送一条数据
		ctx.writeAndFlush("i am client");
	}
}
